# -*- coding: utf-8 -*-
"""
Created on Wed Aug 28 10:22:28 2019

@author: 74655
"""

params={
	"application_id": "loan24332",
	"contract_id": "D020001170925001",
	"collateral": "0",
	"duration": "18",
	"debt_amount": "52943.62",
	"interest_rate": "0.0137",
	"client_name": "Zhou Yongjie",
	"gender": "0",
	"age": "",
	"marriage": "1",
	"id_number": "",
	"education": "",
	"job": "",
	"salary": "",
	"housing": "",
	"car": "",
	"current_address_length": "",
	"current_job_length": "",
	"loan_approv_1m": "3",
	"credit_card_1m": "2",
	"post_loan_1m": "3",
	"personal_check_1m": "2",
	"loan_approv_1y": "7",
	"credit_card_1y": "10",
	"post_loan_1y": "3",
	"personal_check_1y": "3",
	"num_otsd_loan": "2",
	"num_unliquidated_agency": "2",
	"amount_otsd_loan": "56917",
	"balance_otsd_loan": "56762",
	"loan_overdue_2y": "",
	"total_month_overdue_2y": "",
	"longest_month_overdue_2y": "",
	"loan_overdue_5y": "",
	"total_month_overdue_5y": "",
	"longest_month_overdue_5y": "",
	"num_uncancelled_account": "27",
	"num_uncancelled_card_agency": "6",
	"total_credit_granted": "558818",
	"total_credit_used": "143958",
	"overdue_card_account_5y": "2",
	"total_overdue_card_month_5y": "3",
	"longest_overdue_card_month_5y": "",
	"overdue_card_account_2y": "",
	"total_overdue_card_month_2y": "",
	"longest_overdue_card_month_2y": ""
}

import pandas as pd
credit=pd.DataFrame([params])

credit.drop(['gender'],axis=1, inplace=True)
credit.drop(['age'],axis=1, inplace=True)
credit.drop(['id_number'],axis=1, inplace=True)
credit.drop(['education'],axis=1, inplace=True)
credit.drop(['salary'],axis=1, inplace=True)
credit.drop(['housing'],axis=1, inplace=True)
credit.drop(['car'],axis=1, inplace=True)
credit.drop(['current_address_length'],axis=1, inplace=True)
credit.drop(['current_job_length'],axis=1, inplace=True)
credit.drop(['job'],axis=1, inplace=True)

credit.drop(['application_id'],axis=1, inplace=True)  
credit.drop(['contract_id'],axis=1, inplace=True)
credit.drop(['client_name'],axis=1, inplace=True)
credit.drop(['longest_month_overdue_5y'],axis=1, inplace=True)

credit= credit.convert_objects(convert_numeric=True)

credit['loan_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['loan_overdue_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['longest_month_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['longest_overdue_card_month_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['longest_overdue_card_month_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['overdue_card_account_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['total_month_overdue_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['total_month_overdue_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['total_overdue_card_month_2y'] = pd.to_numeric(credit['loan_overdue_2y']) 
credit['total_overdue_card_month_5y'] = pd.to_numeric(credit['loan_overdue_2y']) 

credit['collateral'].fillna(0,inplace=True)
credit['duration'].fillna(18.71,inplace=True)
credit['debt_amount'].fillna(63594.62,inplace=True)
credit['interest_rate'].fillna('0.0135',inplace=True)
credit['marriage'].fillna('0',inplace=True)#谨慎性考虑
credit['loan_approv_1m'].fillna('1',inplace=True)
credit['credit_card_1m'].fillna('1',inplace=True)
credit['post_loan_1m'].fillna('1',inplace=True)
credit['personal_check_1m'].fillna('1',inplace=True)
credit['loan_approv_1y'].fillna('10',inplace=True)
credit['credit_card_1y'].fillna('6',inplace=True)
credit['post_loan_1y'].fillna('11',inplace=True)
credit['personal_check_1y'].fillna('7',inplace=True)
credit['num_otsd_loan'].fillna('6',inplace=True)
credit['num_unliquidated_agency'].fillna('4',inplace=True)
credit['amount_otsd_loan'].fillna('445850',inplace=True)
credit['balance_otsd_loan'].fillna('358749',inplace=True)
credit['loan_overdue_5y'].fillna('0',inplace=True)
credit['total_month_overdue_5y'].fillna('1',inplace=True)
credit['loan_overdue_2y'].fillna('0',inplace=True)
credit['total_month_overdue_2y'].fillna(0,inplace=True)
credit['longest_month_overdue_2y'].fillna('0',inplace=True)
credit['num_uncancelled_account'].fillna('13',inplace=True)
credit['num_uncancelled_card_agency'].fillna('6',inplace=True)
credit['total_credit_granted'].fillna('323878',inplace=True)
credit['total_credit_used'].fillna('118341',inplace=True)
credit['overdue_card_account_5y'].fillna('2',inplace=True)
credit['total_overdue_card_month_5y'].fillna('4',inplace=True)
credit['longest_overdue_card_month_5y'].fillna('1',inplace=True)
credit['overdue_card_account_2y'].fillna('0',inplace=True)
credit['total_overdue_card_month_2y'].fillna('0',inplace=True)
credit['longest_overdue_card_month_2y'].fillna('0',inplace=True)

import numpy as np
credit=pd.DataFrame(credit,dtype=np.float)

import pickle
output = open('whitelist_ext_civ_list.pkl', 'rb')
rst = pickle.load(output)
output.close()

import woe.feature_process as fp
import woe.eval as eval
for r in rst:
    print(r.var_name)
    credit[r.var_name] = fp.woe_trans(credit[r.var_name],r)
    
from sklearn.externals import joblib
clf=model = joblib.load('joblib_model.pkl')

coe=clf.coef_        #特征权值系数，后面转换为打分规则时会用到
y_pred=clf.predict(credit)

import numpy as np
coe_new=coe.reshape(coe[0].shape[0],1)

#计算各个用户的分数
import math
credit['linear_y']=-np.dot(credit,coe_new)
credit['pd']=prob=credit.linear_y.apply(lambda x: 1/(1+math.exp(x)))

factor = 20 / np.log(2)   #factor即为B pd=2/3为临界点时odds=2
offset = 600 - 20 * np.log(2) / np.log(2)  #offset即为A，基准分值为600，odds翻倍分数增加20
credit['score']=score=offset-factor*np.log(credit['pd']/(1-credit['pd']))

print(score)